package org.l.mybatis.core;

import java.util.Collection;

import javax.annotation.PostConstruct;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.l.mybatis.core.exception.MethodDefineException;
import org.l.mybatis.core.sqlsource.DefaultSqlsourceBuilderFactory;
import org.l.mybatis.core.sqlsource.SqlsourceBuilderFactory;

/**
 * Created by 严秋旺 on 2017/7/30.
 */
public class LMybatisScanner {
    private SqlSessionFactory sqlSessionFactory;
//    private MapperBuilderAssistant assistant;
    private SqlsourceBuilderFactory sqlsourceBuilderFactory = new DefaultSqlsourceBuilderFactory();

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public void setDialect(SqlsourceBuilderFactory sqlsourceBuilderFactory) {
        this.sqlsourceBuilderFactory = sqlsourceBuilderFactory;
    }

    @PostConstruct
    public void scan() throws MethodDefineException {
        Configuration configuration = sqlSessionFactory.getConfiguration();
        MapperRegistry mapperRegistry = sqlSessionFactory.getConfiguration().getMapperRegistry();
        Collection<Class<?>> mappers = mapperRegistry.getMappers();

        for (Class<?> type : mappers) {
            MapperSourceBuilder builder = new MapperSourceBuilder(configuration, type, sqlsourceBuilderFactory);
            builder.parse();
        }

    }
}
